/*
 * Copyright (c) 2013, SRI International
 * All rights reserved.
 * Licensed under the The BSD 3-Clause License;
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 * 
 * http://opensource.org/licenses/BSD-3-Clause
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 
 * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 * 
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * 
 * Neither the name of the aic-web-praise nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.sri.ai.lpitools.gwtgui.client.components.menubar;

import com.google.gwt.place.shared.PlaceHistoryMapper;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.MenuBar;
import com.google.gwt.user.client.ui.MenuItem;
import com.google.gwt.view.client.SingleSelectionModel;
import com.sri.ai.lpitools.gwtgui.client.RPCService;
import com.sri.ai.lpitools.gwtgui.client.components.dialogs.MessageDialog;
import com.sri.ai.lpitools.gwtgui.client.components.dialogs.QueryManagerDialog;
import com.sri.ai.lpitools.gwtgui.client.components.dialogs.SelectModelDialog;
import com.sri.ai.lpitools.gwtgui.client.components.manager.AICManager;
import com.sri.ai.lpitools.gwtgui.client.components.manager.ModelManager;
import com.sri.ai.lpitools.gwtgui.client.components.manager.UserManager;
import com.sri.ai.lpitools.gwtgui.client.place.AdminPlace;
import com.sri.ai.lpitools.gwtgui.client.place.EvidencePlace;


/**
 * This class is used to build the menu bar for the AIC gui
 * @author pwolf
 *
 */
public class SRIMenuBar extends MenuBar {

	SingleSelectionModel<String> selectionModel;

	private MenuItem validateModel;
	private final AICManager aicManager;
	private final UserManager userManager;
	private final ModelManager modelManager;
	
	MenuItem openModel;
	MenuItem newModel;
	MenuItem deleteModel;
	MenuItem saveModel;
	MenuItem closeModel;

	private PlaceHistoryMapper placeHistoryMapper;
	
	/**
	 * Constructor that takes the tab
	 * @param manager 
	 * @param tabPanel2
	 */
	public SRIMenuBar(ModelManager mm, AICManager am, UserManager um) {
		super();
		
		this.aicManager = am;
		this.userManager = um;
		this.modelManager = mm;

		
		getElement().getStyle().setProperty("border", "5px solid #e7e7e7");
				
		MenuBar modelBar = new MenuBar(true);
		//create file drop down menu
		MenuItem modelMenu = new MenuItem("Model",modelBar);
		addItem(modelMenu);		

		/*
		 * Create all the sub menus under the model drop down
		 */
		//create open menu
		openModel = new MenuItem("open", new Command() {
			public void execute() {
				openModel();
			}
		});
		//create new menu
		newModel = new MenuItem("new", new Command() {
			public void execute() {
				newModel();
			}
		});
		
		//create close menu
		deleteModel = new MenuItem("delete", new Command() {
			public void execute() {
				deleteModel();
			}
		});	
		
		//create save menu
		saveModel = new MenuItem("save", new Command() {
			public void execute() {
				saveModel();
			}
		});
		
		//create validate menu
		validateModel = new MenuItem("validate", new Command() {
			public void execute() {
				validateModel();
			}
		});	
		
		//create close menu
		closeModel = new MenuItem("close", new Command() {
			public void execute() {
				closeModel();
			}
		});		
		
		//add all menu items to model drop down menu
		modelBar.addItem(openModel);
		modelBar.addItem(newModel);
		modelBar.addItem(saveModel);
		modelBar.addItem(closeModel);		
		modelBar.addItem(validateModel);
		modelBar.addItem(deleteModel);
		
		//create query drop down menu
		MenuBar queryBar = new MenuBar(true);
		MenuItem queryMenu = new MenuItem("Query", queryBar);
		queryMenu.setScheduledCommand( new Command() {
			public void execute() {
				editQuery();
			}
		});
		addItem(queryMenu);	
		
		MenuBar evidenceBar = new MenuBar(true);
		MenuItem evidenceMenu = new MenuItem("Evidence", evidenceBar);
		evidenceMenu.setScheduledCommand( new Command() {
			public void execute() {
				editEvidence();

			}
		});
		addItem(evidenceMenu);	

		MenuBar adminBar = new MenuBar(true);
		MenuItem adminMenu = new MenuItem("Admin", adminBar);
		adminMenu.setScheduledCommand( new Command() {
			public void execute() {
				AdminPlace ep = new AdminPlace();
				String historyToken = placeHistoryMapper.getToken(ep);
				String url = Window.Location.createUrlBuilder().setHash(historyToken).buildString();
				Window.open(url, "_blank", null);
			}
		});
		addItem(adminMenu);	
		
		@SuppressWarnings("unused")
		MenuItem executeItem = addItem("Execute Query", new Command() {
			@Override
			public void execute() {
				aicManager.runQuery();
			}
		});
				
	}
	
	protected void editQuery() {
		if(userManager.isUserLoggedIn())
		{
			QueryManagerDialog dialog = new QueryManagerDialog(modelManager, userManager, RPCService.Util.getInstance());
			dialog.center();
			dialog.show();
		}
		
	}

	protected void editEvidence() {
		if(userManager.isUserLoggedIn())
		{
			EvidencePlace ep = new EvidencePlace("Evidence");
			String historyToken = placeHistoryMapper.getToken(ep);
			String url = Window.Location.createUrlBuilder().setHash(historyToken).buildString();
			Window.open(url, "_blank", null);
		}
	}
		
	protected void openModel(){
		if(userManager.isUserLoggedIn()) {
			SelectModelDialog dialog = new SelectModelDialog(modelManager, userManager, RPCService.Util.getInstance());
			dialog.center();
			dialog.show();		
		} else {
			MessageDialog.showMessageDialog("Sorry you must login before you can open a model");
		}
	}
	
	protected void newModel(){
		if ( userManager.isUserLoggedIn() ) {
			modelManager.setActiveModel(null, false);
		} else {
			MessageDialog.showMessageDialog("Sorry you must login before you can create a new model");
		}		
	}
	protected void deleteModel() {
		if(userManager.isUserLoggedIn()){
			aicManager.deleteModel();
			modelManager.setActiveModel(null, false);
		} else {
			MessageDialog.showMessageDialog("Sorry you must login before you can delete a model");
		}
	}

	/**
	 * Function to validate a model
	 */
	protected void validateModel() {
		AICManager.checkModel(modelManager);
	}

	/**
	 * Function to close model
	 */
	protected void closeModel() {
		if(userManager.isUserLoggedIn()){
			modelManager.setActiveModel(null, false);
		}
	}
	
	/**
	 * This function is used to save the open model
	 */
	protected void saveModel(){
		if(userManager.isUserLoggedIn()){
			AICManager.saveModelBody(modelManager, userManager);
			
		} else {
			MessageDialog.showMessageDialog("Sorry you login before you can save a model");
		}
	}

	public void setPlaceHistoryMapper(PlaceHistoryMapper placeHistoryMapper) {
		this.placeHistoryMapper = placeHistoryMapper;
		
	}
}
